/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */
package com.hepinghospital.user.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.*
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.hepinghospital.user.R

/**
 * @Description:
 * @Author: lzp
 * @CreateDate: 2022/9/30 14:44
 * @UpdateRemark: 更新说明
 */
abstract class BaseDialog : DialogFragment() {
    var mContext: Activity? = null

    lateinit var rootView: View
    var isAnimation = false //用来判断是否多次点击。防止多次执行

    //正常加载
    //onAttach -> onCreate -> onCreateView -> onActivityCreated -> onStart -> onResume
    //正常退出
    //onPause -> onStop -> onDestroyView -> onDestroyView -> onDestroy -> onDetach。
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context as Activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //隐藏标题栏
        setStyle(STYLE_NO_TITLE, R.style.common_selected_dialog)
    }

    abstract fun layoutResource(): Int

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        super.onCreateView(inflater, container, savedInstanceState)
        //点击屏幕，dialog不消失；点击物理返回键dialog消失
        dialog?.setCanceledOnTouchOutside(false)
        //点击屏幕或物理返回键，dialog不消失
        dialog?.setCancelable(false)
        rootView = inflater.inflate(layoutResource(), container, false)
        val window = dialog!!.window!!
        window.setBackgroundDrawableResource(R.color.transparent)
        window.decorView.setPadding(0, 0, 0, 0)
        val wlp = window.attributes
        wlp.gravity = Gravity.CENTER
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = wlp
        return rootView
    }

    override fun show(manager: FragmentManager, tag: String?) {
        show(manager.beginTransaction(), tag, true)
    }

    override fun show(transaction: FragmentTransaction, tag: String?): Int {
        return show(transaction, tag, true)
    }

    fun show(transaction: FragmentTransaction, tag: String?, allowStateLoss: Boolean): Int {
        transaction.add(this, tag)
        return if (allowStateLoss) transaction.commitAllowingStateLoss() else transaction.commit()
    }

    protected var onConfirmListener: ((view: View) -> Unit)? = null
    protected var onCancelListener: (() -> Unit)? = null

    fun setOnDialogListener(
        onConfirmListener: ((view: View) -> Unit),
        onCancelListener: (() -> Unit)? = null
    ): BaseDialog {
        this.onConfirmListener = onConfirmListener
        this.onCancelListener = onCancelListener
        return this
    }

    fun slideToUp(view: View) {
        val translateAnimation = TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f)
        translateAnimation.duration = 400
        translateAnimation.isFillEnabled = true
        translateAnimation.fillAfter = true
        view.startAnimation(translateAnimation)
    }

    fun slideToDown(view: View) {
        val translateAnimation = TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f)
        translateAnimation.duration = 400
        translateAnimation.isFillEnabled = true
        translateAnimation.fillAfter = true
        view.startAnimation(translateAnimation)
        translateAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                //用来判断是否多次点击。防止多次执行
                isAnimation = false
                //弹框消失
                dismiss()
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }
        })
    }

    private fun dialogFinish() {
        if (isAnimation) {
            return
        }
        isAnimation = true
        slideToDown(rootView)
    }
}